package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mapapi.model.LatLng;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.main.goods.bean.BasicsCommodityBase;
import com.zhuozhou.jdyc.wight.dialog.adapter.CouponAdapter;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;

import java.util.List;

/**
 * Created by fangzhou on 2017/1/14.
 * 地图导航dialog
 */

public class CouponDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private CouponAdapter couponAdapter;
    private List<BasicsCommodityBase.DataBean.DiscountBean> list;
    private String str;
    private LatLng latLng;
    private OnDialogInterfaceClickListener sureClickListener;

    public CouponDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
    }

    public CouponDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public CouponDialog setOnSureClickListener(OnDialogInterfaceClickListener sureClickListener) {
        this.sureClickListener = sureClickListener;
        return this;
    }

    @Override
    public void onClick(View v) {

    }

    public interface OnNavigationMapListener {
        void onNavigationClick(Dialog dialog, View v, LatLng latLng, int position);
    }

    public CouponDialog setList(List<BasicsCommodityBase.DataBean.DiscountBean> list) {
        this.list = list;
        return this;
    }

    public CouponDialog setText(String str) {
        this.str = str;
        return this;
    }

    public CouponDialog builder() {
        init();
        return this;
    }

    private void init() { //规格
        View dialogView = LayoutInflater.from(context).inflate(R.layout.coupon_dialog, null);
        TextView tvNoCoupon = dialogView.findViewById(R.id.tv_no_coupon);
        ImageView imageDismiss = dialogView.findViewById(R.id.image_dismiss);
        RecyclerView mRecyclerViewCoupon = dialogView.findViewById(R.id.mRecyclerView_coupon);
        imageDismiss.setOnClickListener(v -> {
            dismiss();
        });
        if (list != null && list.size() > 0) {
            couponAdapter = new CouponAdapter(context);
            if (str != null)
                couponAdapter.setText(str);
            couponAdapter.setList(list);
            couponAdapter.setOnCouponItemClickListener((view, position) -> {
                sureClickListener.onDialogClick(couponAdapter.getList().get(position));
                dismiss();
            });
            mRecyclerViewCoupon.setAdapter(couponAdapter);
            mRecyclerViewCoupon.setLayoutManager(new FullyGridLayoutManager(context, 1) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
        } else {
            tvNoCoupon.setVisibility(View.VISIBLE);
            mRecyclerViewCoupon.setVisibility(View.GONE);
        }
        setContentView(dialogView);
        Window dialogWindow = getWindow();
        if (dialogWindow == null) {
            return;
        }
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(dm);
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    dm.heightPixels * 2 / 3);
        } else {
            dialogWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    public interface OnDialogInterfaceClickListener {
        void onDialogClick(BasicsCommodityBase.DataBean.DiscountBean bean);
    }

}
